package cn.livingCloud.entity.content;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.repository.ArticleReplyMapper;
import cn.livingCloud.repository.ImageTextReplyMapper;
import cn.livingCloud.utils.UuidGenerator;
import cn.livingCloud.wechat.protocol.resp.AbstractRespMsg;
import cn.livingCloud.wechat.protocol.resp.Article;
import cn.livingCloud.wechat.protocol.resp.ArticlesRespMsg;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 图文回复
 * Created by DingJie  on 2015/12/21.
 */
public class ImageTextReply extends ArticlesRespMsg implements Serializable, AbstractReplyContent {
    private transient ImageTextReplyMapper replyMapper = BeanProvider.getBean(ImageTextReplyMapper.class);
    private transient ArticleReplyMapper articleReplyMapper = BeanProvider.getBean(ArticleReplyMapper.class);

    //UUID
    private String uuid = UuidGenerator.generate();

    private String rule_id;

    public ImageTextReply() {
    }

    public ImageTextReply(ReplyRule replyRule) {
        super();
        this.rule_id = replyRule.getUuid();
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    @Override
    public AbstractRespMsg toAbstractRespMsg() {
        Articles = articleReplyMapper.findArticleListByImageTextReplyId(uuid);
        ArticleCount = Articles.size();
        return this;
    }

    @Override
    public void delete() {
        replyMapper.delete(uuid);
        articleReplyMapper.deleteByImageTextReply(uuid);
    }

    @Override
    public List<String> getReplyContent() {
        List<String> stringList = new ArrayList<>(Articles.size());
        for (Article article : Articles) {
            stringList.add(article.getDescription());
        }
        return stringList;
    }

    @Override
    public void setContent(List<String> result) {
        for (String item : result) {
            for (Article article : Articles) {
                article.setDescription(item);
            }
        }
    }

    public String getRule_id() {
        return rule_id;
    }

    public ImageTextReply setRule_id(String rule_id) {
        this.rule_id = rule_id;
        return this;
    }

    @Override
    public List<? extends Article> getArticles() {
        return articleReplyMapper.findArticleListByImageTextReplyId(uuid);
    }
}

